<script setup>
import { artDelArticlesService, artGetArticlesService } from '@/api/article'
import { formatTime } from '@/utils/format'
import { Edit, Delete } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const width = ref('150px')
// 表格数据
const tableData = ref([])
// 数据总数
const total = ref(0)
// 下拉参数
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
// 加载
const loading = ref(false)
// 获取表格数据
const getTableData = async () => {
  loading.value = true
  const res = await artGetArticlesService(params.value)
  tableData.value = res.data.data
  total.value = res.data.total
  loading.value = false
}

// 搜索功能 - 按最新条件，从第一页重新渲染页面
const handleSearch = () => {
  params.value.pagenum = 1
  getTableData()
}
// 重置功能
const handleReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getTableData()
}
const articleDrawerRef = ref()
const onAddArticle = () => {
  articleDrawerRef.value.openDrawer({})
  console.log('执行添加操作')
}
// 编辑
const onEditArticle = (row) => {
  articleDrawerRef.value.openDrawer(row)
}
// 删除
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelArticlesService(row.id)
  ElMessage.success('删除成功')
  // 重新渲染数据
  getTableData()
}

// 处理尺寸改变
const handleSizeChange = (val) => {
  // 重置到第一页
  params.value.pagenum = 1
  params.value.pagesize = val
  // 重新渲染数据
  getTableData()
}
// 处理当前页改变
const handleCurrentChange = (val) => {
  params.value.pagenum = val
  getTableData()
}
// 处理成功事件
const onSuccess = (type) => {
  if (type === 'add') {
    // 发布成功，将最后一页渲染给用户，增加体验感(因为数据会添加到最后一页；向上取整)
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  // 编辑直接将当页渲染即可
  getTableData()
}

onMounted(() => {
  getTableData()
})
</script>

<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle()">发布文章</el-button>
    </template>
    <!-- 表单 -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <channel-select
          v-model="params.cate_id"
          :width="width"
        ></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select style="width: 150px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch()">搜索</el-button>
        <el-button @click="handleReset()">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="title" label="文章标题" width="220">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类" width="180" />
      <el-table-column prop="pub_date" label="发表时间">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" />
      <el-table-column prop="state" label="操作">
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
            circle
            plain
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            @click="onDeleteArticle(row)"
            circle
            plain
          ></el-button>
        </template>
      </el-table-column>
      <!-- 空状态 -->
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 5, 10]"
      :small="small"
      :background="true"
      layout="total, sizes, prev, pager, next"
      :total="total"
      style="margin-top: 15px; justify-content: flex-end"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 抽屉 -->
    <article-drawer
      ref="articleDrawerRef"
      @success="onSuccess"
    ></article-drawer>
  </page-container>
</template>
